package com.cn.jx.manager.controller;

import java.text.MessageFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cn.jx.common.controller.BaseController;
import com.cn.jx.common.util.jpaQuery.ConvertCondition;
import com.cn.jx.common.util.jpaQuery.PageCondition;
import com.cn.jx.common.util.jpaQuery.QueryCondition;
import com.cn.jx.common.util.page.PageObj;
import com.cn.jx.common.vo.SessionUser;
import com.cn.jx.manager.form.JobForm;
import com.cn.jx.manager.po.Job;
import com.cn.jx.manager.service.mtn.IJobServiceMtn;
import com.cn.jx.manager.service.query.IJobServiceQry;
import com.jx.enumType.DataGrideTypeEnum;
import com.jx.enumType.ResourceEnum;
import com.jx.util.FormValidat;
import com.jx.util.MapObject;
import com.jx.util.Response;
import com.jx.util.Utility;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月1日
 * <p>功能描述：
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Controller
@RequestMapping("/admin/uoj/job/")
public class JobController  extends BaseController{
	 
	private static final String _PAGE_PATH="userorgjob/";
	
	/**定义{@link IJobService jobService <tt>岗位接口</tt>}*/
	@Autowired
	private IJobServiceMtn jobServiceMtn;
	/**定义{@link IJobService jobService <tt>岗位接口</tt>}*/
	@Autowired
	private IJobServiceQry jobServiceQry;
	
	/**
	 * 
	* @Title:userList
	* @Description:TODO(跳转到页面)
	* <p>邮箱地址: 524577360@qq.com
	* @throws
	 */
	 @RequestMapping("list/{path}")
	 public String userList(Model model, HttpServletRequest request,@PathVariable(value = "path") String path) {
		 SessionUser sessUser = getSessionUser(request);
		 setModelColumns(model, DataGrideTypeEnum.JOB_MGR);
		 setModelPermit(model,ResourceEnum.JOB_MGR,sessUser.getResourceCodeList());
	     return MessageFormat.format("{0}{1}", _PAGE_PATH,"recycle".equals(path)?"job/jobrecycle":"joblist");
	 }
	 
	 /**
	  * 
	 * @Title:editWindow
	 * @Description:TODO(跳转到编辑)
	 * <p>邮箱地址: 524577360@qq.com
	 * String
	 * @throws
	  */
	 @RequestMapping("edit")
	 public String editWindow(Model model,HttpServletRequest request,String id) {
		 //id存在 则为修改编辑
		 //parentcode 为空则增加根 否则增加子项目
		 Job res= null;
		 if(Utility.isNotBlank(id)){
			 res= jobServiceQry.getJobById(id);
		 }
		 model.addAttribute("res",res);
	     return _PAGE_PATH+"job/jobedit";
	 }
	 
	 
	 /**
	  * 岗位列表分页
	  * @param model
	  * @param request
	  * @param page
	  * @param rows
	  * @return
	  */
	 @RequestMapping("listData/{path}")
	 @ResponseBody
	 public PageObj<MapObject> jobList(Model model,@PathVariable(value = "path") String path, PageCondition page) {
		 QueryCondition cond = ConvertCondition.initCondition(page, path);
		try {
			 PageObj<MapObject> pageuser = jobServiceQry.findJobByPageAndParams(cond);
			 return pageuser;
		} catch (Exception e) {
			log.debug(e.getMessage());
			e.printStackTrace();
		}
		return null;
	     
	 }
	 
	/**
	 * 
	* @Title:addJob
	* @Description:TODO(添加岗位)
	* <p>邮箱地址: 524577360@qq.com
	* @throws
	 */
	@RequestMapping("addOrUpdate")
	@ResponseBody
	public Response addJob(Model model, HttpServletRequest request, JobForm jobForm) {
		SessionUser sessUser = getSessionUser(request);
		try {
			// 表单验证
			FormValidat.exec(jobForm);
			jobForm.setOperatUser(sessUser.getUserId());
			jobServiceMtn.saveOrUpdateJob(jobForm);
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	 
	/**
	 * 
	* @Title:delJob
	* @Description:TODO(删除岗位根据id 标记删除)
	* <p>邮箱地址: 524577360@qq.com
	* Response
	* @throws
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Response delJob(Model model, HttpServletRequest request, String ids) {
		SessionUser sessUser = getSessionUser(request);
		try {
			FormValidat.validId(ids);
			List<String> list = JSON.parseArray(ids, String.class);
			jobServiceMtn.delJob(list, sessUser.getUserId());
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	
	/**
	 * 
	* @Title:removeJob
	* @Description:TODO(回收站删除)
	* <p>邮箱地址: 524577360@qq.com
	* @throws
	 */
	@RequestMapping("remove")
	@ResponseBody
	public Response removeJob(Model model, HttpServletRequest request, String ids) {
		SessionUser sessUser = getSessionUser(request);
		try {
			// 表单验证
			FormValidat.validId(ids);
			// 表单验证
			List<String> list = JSON.parseArray(ids, String.class);
			jobServiceMtn.removeJob(list, sessUser.getUserId());
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	
	/**
	 * 
	* @Title:recoverJob
	* @Description:TODO(回收站回收)
	* <p>邮箱地址: 524577360@qq.com
	* @throws
	 */
	@RequestMapping("recover")
	@ResponseBody
	public Response recoverJob(Model model, HttpServletRequest request, String ids) {
		SessionUser sessUser = getSessionUser(request);
		try {
			// 表单验证
			FormValidat.validId(ids);
			// 表单验证
			List<String> list = JSON.parseArray(ids, String.class);
			jobServiceMtn.recoverJob(list, sessUser.getUserId());
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	
	/**
	 * 
	* @Title:removeAllJob
	* @Description:TODO(清空)
	* <p>邮箱地址: 524577360@qq.com
	* @throws
	 */
	@RequestMapping("removeAll")
	@ResponseBody
	public Response removeAllJob(Model model, HttpServletRequest request) {
		SessionUser sessUser = getSessionUser(request);
		try {
			jobServiceMtn.removeJobAll(sessUser.getUserId());
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	 
	 /**
	  * 
	 * @Title:getJobById
	 * @Description:TODO(根据岗位ID获取岗位信息)
	 * <p>邮箱地址: 524577360@qq.com
	 * @throws
	  */
	 @RequestMapping("get")
	 @ResponseBody
	 public Response getJobById(Model model,HttpServletRequest request,String id) {
		 Job of= jobServiceQry.getJobById(id);
	     return Response.Success(of);
	 }
	 
	 /**
	  * 
	 * @Title:getAllJob
	 * @Description:TODO(获取所有job)
	 * <p>邮箱地址: 524577360@qq.com
	 * List<Job>
	 * @throws
	  */
	 @RequestMapping("getAllJob")
	 @ResponseBody
	 public List<Job> getAllJob(Model model,HttpServletRequest request) {
		 List<Job> res= jobServiceQry.findAllJob();
	     return res;
	 }
}
